package com.pi.base.serializer;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

/** 
* @author chenmfa
* @version 创建时间：2017年7月4日 上午10:40:56 
* @description
*/
public class ObjectMappingresolver extends ObjectMapper{

  private static final long serialVersionUID = 927472513991864444L;
  
  public ObjectMappingresolver(){  
    super();  
    // 允许单引号  
    this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);  
    // 字段和值都加引号  
    this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  
    // 数字也加引号  
    this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);  
    this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true); 
    // 空值处理为空串  
    SerializerProvider sp = this.getSerializerProvider();
    sp.setNullValueSerializer(new JsonSerializer<Object>(){
      @Override
      public void serialize(Object value, JsonGenerator jg,
        SerializerProvider sp) throws IOException,
        JsonProcessingException {
        jg.writeString("");  
      }
    });
  }  
}
